using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Book.Blog.Core.DTOs;
using Book.Blog.Core.Interfaces;
using System.Threading.Tasks;

namespace Book.Blog.API.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    [Authorize(Roles = "Admin")]
    public class EmailController : ControllerBase
    {
        private readonly IEmailService _emailService;

        public EmailController(IEmailService emailService)
        {
            _emailService = emailService;
        }

        /// <summary>
        /// 发送邮件（需管理员）
        /// </summary>
        /// <param name="message">邮件内容</param>
        /// <returns>发送结果</returns>
        /// <remarks>
        /// 示例请求:
        /// {
        ///   "to": "user@example.com",
        ///   "subject": "主题",
        ///   "body": "内容"
        /// }
        /// </remarks>
        [HttpPost("send")]
        public async Task<IActionResult> SendEmail([FromBody] EmailMessageDTO message)
        {
            await _emailService.SendEmailAsync(message);
            return Ok();
        }

        /// <summary>
        /// 发送模板邮件（需管理员）
        /// </summary>
        /// <param name="to">收件人邮箱</param>
        /// <param name="templateName">模板名称</param>
        /// <param name="model">模板参数</param>
        /// <returns>发送结果</returns>
        [HttpPost("send-template")]
        public async Task<IActionResult> SendEmailWithTemplate(
            [FromQuery] string to,
            [FromQuery] string templateName,
            [FromBody] object model)
        {
            await _emailService.SendEmailWithTemplateAsync(to, templateName, model);
            return Ok();
        }
    }
} 